package com.qinggeng.project.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.qinggeng.common.enums.UserConsumeEnum;
import com.qinggeng.project.system.domain.OrdersGoods;
import com.qinggeng.project.system.domain.UserConsume;
import com.qinggeng.project.system.domain.UserWallet;
import com.qinggeng.project.system.domain.UserWishGift;
import com.qinggeng.project.system.domain.json.Percentage;
import com.qinggeng.project.system.mapper.OrdersGoodsMapper;
import com.qinggeng.project.system.mapper.UserWalletMapper;
import com.qinggeng.project.system.service.ISysConfigService;
import com.qinggeng.project.system.service.IUserConsumeService;
import com.qinggeng.project.system.service.IUserWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

import static com.qinggeng.project.system.service.impl.OrdersGoodsServiceImpl.PERCENTAGE_KEY;

/**
 * 用户钱包Service业务层处理
 *
 * @author qinggeng
 * @date 2020-06-26
 */
@Service
public class UserWalletServiceImpl implements IUserWalletService
{
    @Resource
    private UserWalletMapper userWalletMapper;
    @Autowired
    private IUserConsumeService userConsumeService;
    @Resource
    private OrdersGoodsMapper ordersGoodsMapper;
    @Autowired
    private ISysConfigService sysConfigService;
    /**
     * 查询用户钱包
     *
     * @param walletId 用户钱包ID
     * @return 用户钱包
     */
    @Override
    public UserWallet selectUserWalletById(Long walletId)
    {
        return userWalletMapper.selectUserWalletById(walletId);
    }

    /**
     * 查询用户钱包列表
     *
     * @param userWallet 用户钱包
     * @return 用户钱包
     */
    @Override
    public List<UserWallet> selectUserWalletList(UserWallet userWallet)
    {
        return userWalletMapper.selectUserWalletList(userWallet);
    }

    /**
     * 新增用户钱包
     *
     * @param userWallet 用户钱包
     * @return 结果
     */
    @Override
    public int insertUserWallet(UserWallet userWallet)
    {
        return userWalletMapper.insertUserWallet(userWallet);
    }

    /**
     * 修改用户钱包
     *
     * @param userWallet 用户钱包
     * @return 结果
     */
    @Override
    public int updateUserWallet(UserWallet userWallet)
    {
        return userWalletMapper.updateUserWallet(userWallet);
    }

    /**
     * 批量删除用户钱包
     *
     * @param walletIds 需要删除的用户钱包ID
     * @return 结果
     */
    @Override
    public int deleteUserWalletByIds(Long[] walletIds)
    {
        return userWalletMapper.deleteUserWalletByIds(walletIds);
    }

    /**
     * 删除用户钱包信息
     *
     * @param walletId 用户钱包ID
     * @return 结果
     */
    @Override
    public int deleteUserWalletById(Long walletId)
    {
        return userWalletMapper.deleteUserWalletById(walletId);
    }

    @Override
    public UserWallet selectUserWalletByUserId(Long userId) {
        return userWalletMapper.selectUserWalletByUserId(userId);
    }

    @Override
    public int addWallet(Long userId, BigDecimal price,Long ordersId,UserConsumeEnum enumType) {

        UserWallet userWallet = selectUserWalletByUserId(userId);
        if(null==userWallet){
           return 0;
        }
        int rows=userWalletMapper.userWalletAdd(userWallet.getWalletId(),price);
        if(rows<1){
            return 0;
        }
        //骑手收益添加到收支明细
        return userConsumeService.addUserConsume(userId,price,ordersId,enumType,"+");
    }



    @Override
    public int subtractWallet(Long userId, BigDecimal price, Long ordersId, UserConsumeEnum enumType) {
        UserWallet userWallet=selectUserWalletByUserId(userId);
        if(null==userWallet){
            return 0;
        }
        int rows=userWalletMapper.userWalletReduce(userWallet.getWalletId(),price);
        if(rows<1){
            return 0;
        }
            //提成扣除添加到收支明细
         return userConsumeService.addUserConsume(userId,price,ordersId,enumType,"-");
    }

    @Override
    public int addOrdersGoodsSharePercentage(Long ordersId){

        String percentageJson=sysConfigService.getConfigValue(PERCENTAGE_KEY);

        Percentage percentageConfig= JSON.parseObject(percentageJson,Percentage.class);

        int rows=0;
        List<OrdersGoods> ordersGoodsList=ordersGoodsMapper.selectOrderGoodListByOrdersId(ordersId);
        for (OrdersGoods ordersGoods : ordersGoodsList) {
            if (null == ordersGoods.getCourierId() || ordersGoods.getCourierId() == 0) {
                continue;
            }
            BigDecimal percentage;
            if (percentageConfig.isFixed()) {
                percentage=percentageConfig.getPercentage();
            } else {
                percentage=ordersGoods.getGoodsPrice().multiply(percentageConfig.getPercent());
            }
            int result=this.addWallet(ordersGoods.getCourierId(),percentage,ordersId,UserConsumeEnum.订单提成);
            if(result>0){
                rows++;
            }
        }
        return rows;

    }

    @Override
    public int resetDayPrice() {
        return userWalletMapper.resetDayPrice();
    }
}
